// pages/productLineList/productLineList.js
import * as API_Classify from '../../api/classify'
import * as API_Product from '../../api/product'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classHeight:0,
    paramsArr:[],
    params:{
      longitude:'',
      latitude:'',
      newUseFlag:'Y',
      pageNum:1,
      pageSize:20,
      classifyParentId:'',
      classifyId:'',
      cityCode:''
    },
    newProductList:[],
    finished:false,
    parentClassify:[],
    secondClassify:[],
    choiceFirst:0,
    choiceSecond:0
  },
  choiceFirst(e){
    if(e.currentTarget.dataset.index == this.data.choiceFirst){
      return
    }
    let item = this.data.parentClassify[e.currentTarget.dataset.index]
    this.setData({
      choiceFirst:e.currentTarget.dataset.index,
      'params.classifyParentId':item.id,
      'params.classifyId':item.child.length>0?item.child[0].id : '',
      'params.pageNum':1,
      choiceSecond:0,
      newProductList:[],
      secondClassify:item.child,
      finished:false
    })
    this.getList()
  },
  choiceSecond(e){
    if(e.currentTarget.dataset.index == this.data.choiceSecond){
      return
    }
    let item = this.data.secondClassify[e.currentTarget.dataset.index]
    this.setData({
      'params.pageNum':1,
      'params.classifyId':item.id,
      choiceSecond:e.currentTarget.dataset.index,
      newProductList:[],
      finished:false
    })
    this.getList()
  },
  getList(){
    API_Product.getList(this.data.params).then(res=>{
      let arr = this.data.newProductList
      arr.push(...res.data.rows)
      this.setData({
        newProductList:arr,
        finished:res.data.lastPage
      })
    })
  },
  goDetail(e){
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?id='+e.currentTarget.dataset.id,
    })
  },
  calHeight:function(){

    let query = wx.createSelectorQuery();
    query.select('.classView').boundingClientRect(rect=>{
      if(rect){
        let clientHeight = rect.height;
        // let clientWidth = rect.width;
        // let ratio = 750 / clientWidth;
        // let height = clientHeight * ratio;
      
        this.setData({classHeight:clientHeight})
      }
     
    }).exec();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let globalData=getApp().globalData;
    let navHeadHeightpx=globalData.navHeadHeightpx;
    let windowHeight=globalData.windowHeight;
    this.setData({navHeadHeightpx:navHeadHeightpx,windowHeight:windowHeight})
    this.setData({
      'params.latitude':getApp().globalData.latitude,
      'params.longitude':getApp().globalData.longitude,
      'params.cityCode':options.cityCode

    })
    this.loadClassify()
  },

  loadClassify(){
    API_Classify.getList().then(res=>{
      this.setData({
        parentClassify:res.data,
        'params.classifyParentId':res.data[0].id,
        secondClassify:res.data[0].child,
        'params.classifyId':res.data[0].child[0].id,
      })
      this.getList()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
this.calHeight();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.finished){
      return
    }
    var page = this.data.params.pageNum + 1
    this.setData({
      'params.pageNum':page
    })
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})